package com.cadre.system.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cadre.common.utils.annotation.A19BTag;
import com.cadre.common.utils.annotation.A19Tag;
import com.cadre.common.utils.oldutil.DateUtil;
import com.cadre.common.utils.oldutil.PageUtils;
import com.cadre.system.domain.officer.A01;
import com.cadre.system.domain.officer.A19;
import com.cadre.system.domain.officer.ResumeFormatVO;
import com.cadre.system.mapper.A01Mapper;
import com.cadre.system.mapper.A19BMapper;
import com.cadre.system.mapper.A19Mapper;
import com.cadre.system.olddomain.ResumeParse;
import com.cadre.system.service.A01Service;
import com.cadre.system.service.A19Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

@Service("A19Service")
public class A19ServiceImpl extends ServiceImpl<A19Mapper, A19> implements A19Service {
    @Autowired
    private A19Mapper a19Mapper;
    @Autowired
    private A19BMapper a19BMapper;
    @Autowired
    private A01Mapper a01Mapper;
    @Autowired
    private A01Service a01Service;
    private static final String DEFAULT_SPACE = "                  ";
    private static final String LINE_FEED = "\n";

    @Override // com.tdqy.cadre.modular.profess.service.A19Service
    public PageUtils queryPage(Map<String, Object> params) {
//        return new PageUtils(selectPage(new Query(params).getPage(), new EntityWrapper()));
        return new PageUtils(new Page<>());
    }

    @Override // com.tdqy.cadre.modular.profess.service.A19Service
    public List<A19> selectByList(Map<String, Object> map) {
        return this.a19Mapper.selectByList(map);
    }

    @Override // com.tdqy.cadre.modular.profess.service.A19Service
    @Transactional(rollbackFor = {Exception.class})
    public int updateByPrimaryKeySelective(A19 a19) {
        return this.a19Mapper.updateByPrimaryKeySelective(a19);
    }

    @Override // com.tdqy.cadre.modular.profess.service.A19Service
    @Transactional(rollbackFor = {Exception.class})
    public int update_A01(A01 a01, String a0183, String a0183b) {
        Date a1905 = selectMaxA1905(a01.getFlowid());
        String a1930 = selectMaxA1930(a01.getFlowid());
        String a1970 = selectMaxA1970(a01.getFlowid());
        a01.setA0179(a1930);
        a01.setA0198(a1970);
        a01.setA0185(a1905);
        a01.setA0183(a0183);
        a01.setA0183b(a0183b);
        return this.a01Service.updateXianz(a01);
    }

    @Override // com.tdqy.cadre.modular.profess.service.A19Service
    public List<A19> praseResume(String resume) {
        ResumeParse parse = ResumeParse.getInstance();
        List<A19> list = new ArrayList<>();
        try {
            list = parse.parseResume(resume);
        } catch (Exception e) {
            System.out.println("prase resume error:" + e.getMessage());
        }
        return list;
    }

    public String getXianzhi(A01 entity) {
        Date beginDate = entity.getA0185();
        String xianzhi = entity.getA0183();
        if (xianzhi == null || xianzhi.equals("")) {
            return "";
        }
        if (beginDate == null && xianzhi != null && !xianzhi.equals("")) {
            return DEFAULT_SPACE + xianzhi;
        }
        return DateUtil.dateToString(beginDate, DateUtil.MONTG_DATE_FORMAT) + "--         " + xianzhi;
    }

    @Override // com.tdqy.cadre.modular.profess.service.A19Service
    public String fromatResume(JSONObject json) {
        ResumeFormatVO next;
        List<String> resumeList = new ArrayList<>();
        List<ResumeFormatVO> a19bList = JSON.parseArray(json.getJSONArray("resumelist").toString(),ResumeFormatVO.class);
        String personId = json.getString("personId");
        List<ResumeFormatVO> a19List = buildFormatResume(a19bList, this.baseMapper.selectByPersonid(personId));
        A01 entity = (A01) this.a01Mapper.selectById(personId);
        new ResumeFormatVO();
        String oprater = "";
        for (int i = 0; i < a19List.size(); i++) {
            ResumeFormatVO curr = a19List.get(i);
            if (i == a19List.size() - 1) {
                next = null;
            } else {
                next = a19List.get(i + 1);
            }
            if (isSameUnit(curr, next)) {
                if (oprater.equals("")) {
                    StringBuilder sb = new StringBuilder();
                    sb.append(curr.a1905).append("--").append(curr.a1910.equals("") ? "       " : curr.a1910).append("  ").append(curr.a1916).append(curr.a1965).append(curr.a1920).append("、").append(next.a1965).append(next.a1920).append(formatRemark(next.remark));
                    oprater = sb.toString();
                } else {
                    if (next.remark != null && !next.remark.equals("")) {
                        oprater = oprater.replace(formatRemark(next.remark), "");
                    }
                    oprater = oprater + "、" + next.a1965 + next.a1920 + formatRemark(next.remark);
                }
            } else if (isSameZhiwu(curr, next)) {
                if (oprater.equals("")) {
                    StringBuilder sb2 = new StringBuilder();
                    sb2.append(curr.a1905).append("--").append(curr.a1910.equals("") ? "       " : curr.a1910).append("  ").append(curr.a1916).append(curr.a1965).append("、").append(next.a1916).append(next.a1965).append("$seat").append(next.a1920).append(formatRemark(next.remark));
                    oprater = sb2.toString();
                } else {
                    if (next.remark != null && !next.remark.equals("")) {
                        oprater = oprater.replace(formatRemark(next.remark), "");
                    }
                    oprater = oprater.replace("$seat", "、" + next.a1916 + next.a1965 + "$seat") + formatRemark(next.remark);
                }
            } else if (isSamePoit(curr, next)) {
                if (oprater.equals("")) {
                    StringBuilder sb3 = new StringBuilder();
                    sb3.append(curr.a1905).append("--").append(curr.a1910.equals("") ? "       " : curr.a1910).append("  ").append(curr.a1916).append(curr.a1965).append(curr.a1920).append(",").append(next.a1916).append(next.a1965).append(next.a1920).append(formatRemark(next.remark));
                    oprater = sb3.toString();
                } else {
                    if (next.remark != null && !next.remark.equals("")) {
                        oprater = oprater.replace(formatRemark(next.remark), "");
                    }
                    oprater = oprater + "," + next.a1916 + next.a1965 + next.a1920 + formatRemark(next.remark);
                }
            } else if (oprater.equals("")) {
                StringBuilder sb4 = new StringBuilder();
                sb4.append(curr.a1905).append("--").append(curr.a1910.equals("") ? "       " : curr.a1910).append("  ").append(curr.a1916).append(curr.a1965).append(curr.a1920).append(formatRemark(curr.remark));
                resumeList.add(sb4.toString());
                oprater = "";
            } else {
                resumeList.add(oprater.replace("$seat", ""));
                oprater = "";
            }
        }
        resumeList.add(getXianzhi(entity));
        resumeList.add(getDaiBiaoWeiyuan(entity));
        for (String str : resumeList) {
            System.out.println(str);
        }
        return finalFormatResume(resumeList);
    }

    private boolean isSameUnit(ResumeFormatVO curr, ResumeFormatVO next) {
        if (next == null) {
            return false;
        }
        return curr.a1905.equals(next.a1905) && (curr.a1910 == null ? "" : curr.a1910).equals(next.a1910 == null ? "" : next.a1910) && curr.a1916.equals(next.a1916);
    }

    private boolean isSameZhiwu(ResumeFormatVO curr, ResumeFormatVO next) {
        if (next == null) {
            return false;
        }
        return curr.a1905.equals(next.a1905) && (curr.a1910 == null ? "" : curr.a1910).equals(next.a1910 == null ? "" : next.a1910) && curr.a1920.equals(next.a1920);
    }

    private boolean isSamePoit(ResumeFormatVO curr, ResumeFormatVO next) {
        if (next == null) {
            return false;
        }
        return curr.a1905.equals(next.a1905) && (curr.a1910 == null ? "" : curr.a1910).equals(next.a1910 == null ? "" : next.a1910);
    }

    private List<ResumeFormatVO> buildFormatResume(List<ResumeFormatVO> a19List, List<A19> list) {
        for (A19 a19 : (List<A19>) list.stream().filter(t -> {
            return t.getA1910() != null;
        }).collect(Collectors.toList())) {
            ResumeFormatVO format = new ResumeFormatVO();
            format.a1905 = a19.getA1905() == null ? "" : DateUtil.dateToString(a19.getA1905(), DateUtil.MONTG_DATE_FORMAT);
            format.a1910 = a19.getA1910() == null ? "" : DateUtil.dateToString(a19.getA1910(), DateUtil.MONTG_DATE_FORMAT);
            format.a1916 = a19.getA1916() == null ? "" : a19.getA1916();
            format.a1920 = a19.getA1920() == null ? "" : a19.getA1920();
            format.a1965 = a19.getA1965() == null ? "" : a19.getA1965();
            format.remark = a19.getRemark() == null ? "" : a19.getRemark();
            a19List.add(format);
        }
        return a19List;
    }

    private String finalFormatResume(List<String> list) {
        StringBuilder sb = new StringBuilder();
        for (String str : list) {
            sb.append(str).append("\r\n");
        }
        return sb.toString();
    }

    private boolean isDateBegin(String str) {
        return Pattern.compile("^[0-9]{4}\\.{1}[0-9]{2}--[0-9]{4}\\.{1}[0-9]{2}.*".toString()).matcher(str).matches();
    }

    private String formatRemark(String remark) {
        String remark2;
        if (remark == null || remark.equals("")) {
            return "";
        }
        if (isDateBegin(remark)) {
            remark2 = "\r\n                  (" + remark + ")";
        } else {
            remark2 = "(" + remark + ")";
        }
        return remark2;
    }

    private String getDaiBiaoWeiyuan(A01 entity) {
        StringBuilder sb = new StringBuilder();
        if (entity.getA0199() == null || entity.getA0199().equals("")) {
            return "";
        }
        sb.append(LINE_FEED).append(DEFAULT_SPACE).append(entity.getA0199());
        return sb.toString();
    }

    public String getNewResumeWorld(String resume, String lastBeginTime, String a0183) {
        Matcher matcher = Pattern.compile("\\d{4}\\.\\d{2}--\\s{4,}.*").matcher(resume);
        String lastItem = "";
        while (matcher.find()) {
            lastItem = matcher.group();
        }
        return matcher.replaceAll(replaceTime(lastItem, lastBeginTime, a0183));
    }

    public String replaceTime(String str, String lastBeginTime, String a0183) {
        String optionItem = Pattern.compile("(?<=--)\\s{7}").matcher(str).replaceAll(lastBeginTime);
        StringBuilder sb = new StringBuilder();
        sb.append(optionItem).append(LINE_FEED).append(lastBeginTime).append("--         ").append(a0183);
        return sb.toString();
    }

    private String getDemoString(List<A19> list) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            if (i == 0) {
                sb.append(list.get(i).getA1916());
                sb.append(list.get(i).getA1965() == null ? "" : list.get(i).getA1965());
                sb.append(list.get(i).getA1920());
            } else {
                sb.append("、");
                sb.append(list.get(i).getA1965() == null ? "" : list.get(i).getA1965());
                sb.append(list.get(i).getA1920());
            }
        }
        return sb.toString();
    }

    public String GetDate(String oriText) {
        return oriText.replaceAll("[一-龥]+", "").replaceAll("[，。、()（）!！]+", "").trim();
    }

    public String GetChineseWord(String oriText) {
        return Pattern.compile("[^一-龥，。、（）]").matcher(oriText).replaceAll("").replaceAll("[，。、()（）]+", "").trim();
    }

    @Override // com.tdqy.cadre.modular.profess.service.A19Service
    public List<A19> queryInitList(String personId) {
        return this.a19BMapper.queryInitList(personId);
    }

    @Override // com.tdqy.cadre.modular.profess.service.A19Service
    @Transactional(rollbackFor = {Exception.class})
    public int deleteA19BById(String personId) {
        return this.a19BMapper.deleteA19BById(personId);
    }

    @Override // com.tdqy.cadre.modular.profess.service.A19Service
    @Transactional(rollbackFor = {Exception.class})
    @A19BTag
    public int saveA19BByList(List<A19> a19List, String personId) {
        return this.a19BMapper.saveA19BByList(a19List, personId);
    }

    @Override // com.tdqy.cadre.modular.profess.service.A19Service
    @Transactional(rollbackFor = {Exception.class})
    public int saveA19ByList(List<A19> a19List) {
        int i = 0;
        for (A19 a19 : a19List) {
            i = saveA19(a19);
        }
        return i;
    }

    @Override // com.tdqy.cadre.modular.profess.service.A19Service
    public String checkAuthByPersonid(String userId, String personId) {
        return this.a19Mapper.checkAuthByPersonid(userId, personId);
    }

    @Override // com.tdqy.cadre.modular.profess.service.A19Service
    public String checkAuthByUnitid(String userId, String unitId) {
        return this.a19Mapper.checkAuthByUnitid(userId, unitId);
    }

    @Override // com.tdqy.cadre.modular.profess.service.A19Service
    @Transactional(rollbackFor = {Exception.class})
    public int updateOrderByPersonArray(List<String> personArray, String unitid) {
        return this.a19Mapper.updateOrderByPersonArray(personArray, unitid);
    }

    @Override // com.tdqy.cadre.modular.profess.service.A19Service
    public List<A19> querySegmentList(String personId) {
        return this.a19BMapper.querySegmentList(personId);
    }

    @Override // com.tdqy.cadre.modular.profess.service.A19Service
    public String checkIsNullPositionByPersonid(String personId) {
        return this.a19Mapper.checkIsNullPositionByPersonid(personId);
    }

    @Override // com.tdqy.cadre.modular.profess.service.A19Service
    public Date selectMaxA1905(String personid) {
        return this.a19Mapper.selectMaxA1905(personid);
    }

    @Override // com.tdqy.cadre.modular.profess.service.A19Service
    public String selectMaxA1970(String personid) {
        return this.a19Mapper.selectMaxA1970(personid);
    }

    @Override // com.tdqy.cadre.modular.profess.service.A19Service
    public String selectMaxA1930(String personid) {
        return this.a19Mapper.selectMaxA1930(personid);
    }

    @Override // com.tdqy.cadre.modular.profess.service.A19Service
    @Transactional(rollbackFor = {Exception.class})
    public Integer updateByUidAndPid(Map<String, Object> map) {
        return this.a19Mapper.updateByUidAndPid(map);
    }

    @Override // com.tdqy.cadre.modular.profess.service.A19Service
    @Transactional(rollbackFor = {Exception.class})
    public int update_A01_A0177b(A01 a01, String xz) {
        a01.setA0177b(getNewResumeWorld(a01.getA0177b(), DateUtil.dateToStr(selectMaxA1905(a01.getFlowid())), xz));
        return this.a01Service.updateXianz(a01);
    }

    @Override // com.tdqy.cadre.modular.profess.service.A19Service
    @Transactional(rollbackFor = {Exception.class})
    public A01 update_A0177b(String personid, String a0183) {
        A01 a01 = (A01) this.a01Service.getById(personid);
        String a0177b = a01.getA0177b();
        Matcher matcher = Pattern.compile("\\d{4}\\.\\d{2}(?=(--)\\s{7})").matcher(a0177b);
        List<String> lastItem = new ArrayList<>();
        while (matcher.find()) {
            lastItem.add(matcher.group());
        }
        String last_time_string = String.join(",", (CharSequence[]) lastItem.toArray(new String[lastItem.size()]));
        String time = DateUtil.dateToStr(selectMaxA1905(a01.getFlowid()));
        if (a0177b.contains(time + "--         " + a0183)) {
            return a01;
        }
        if (!last_time_string.contains(time) || a0177b.contains(a0183)) {
            a01.setA0177b(getNewResumeWorld(a0177b, time, a0183));
            this.a01Service.updateXianz(a01);
        } else {
            a01.setA0177b(Pattern.compile("(?<=\\s{9,})[\\S]*").matcher(a0177b).replaceAll(a0183));
            this.a01Service.updateXianz(a01);
        }
        return a01;
    }

    @Override // com.tdqy.cadre.modular.profess.service.A19Service
    @Transactional
    public int deleteBatchPersonid(String tableName, List<String> ids) {
        return this.a19Mapper.deleteBatchPersonid(tableName, ids);
    }

    @Override // com.tdqy.cadre.modular.profess.service.A19Service
    @Transactional(rollbackFor = {Exception.class})
    public int deleteByPersonid(String personid) {
        return this.a19Mapper.deleteByPersonid(personid);
    }

    @Override // com.tdqy.cadre.modular.profess.service.A19Service
    @A19Tag("更新")
    public int saveA19(A19 a19) {
        return this.a19Mapper.insertSelective(a19);
    }

    @Override // com.tdqy.cadre.modular.profess.service.A19Service
    public List<A19> selectListByUnit(String unitId) {
        return this.a19Mapper.selectListByUnit(unitId);
    }
}
